package com.mk.plugin.function;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SelectFunction extends Functions {
    public SelectFunction(String fieldName, String labelText) {
        super(fieldName);

        if (labelText != null) {
            this.setLabelText(labelText);
        }
    }

    @Override
    public List<String> getSubviewProps() {
        return Arrays.asList(
                "searchCondition",
                String.format("%sList", this.getFieldName()),
                String.format("onChange%s", this.getFieldFirstUpperCamelCaseName())
        );
    }

    private List<String> needComponent = Arrays.asList(
            "MKToolbar",
            "MKToolbarLeftAction"
    );

    @Override
    public List<String> getNeedComponent() {
        return needComponent;
    }

    private List<String> needMapToProps = Arrays.asList(
            "searchCondition",
            String.format("%sList", this.getFieldName())
    );

    @Override
    public List<String> getNeedMapToProps() {
        return needMapToProps;
    }

    @Override
    public List<String> getSearchConditionInitState(Map<String, Object> properties) {
        return Arrays.asList(this.getFieldName() + ": 0");
    }

    @Override
    public String getLeftActionComponentString() {
        String result = String.format(" <Select name=\"%s\"\n" +
                "                            options={%sList}\n" +
                "                            value={searchCondition.%s}\n" +
                "                            valueKey=\"id\"\n" +
                "                            labelKey=\"name\"\n" +
                "                            onChange={onChange%s}\n" +
                "                            className=\"right-space select-width\"\n" +
                "                    />", this.getFieldName(), this.getFieldName(), this.getFieldName(), this.getFieldFirstUpperCamelCaseName());

        if (this.getLabelText() != null) {
            result = String.format("<div className=\"toolbar-label\">%s</div>\n", this.getLabelText()) + result;
        }
        return result;
    }

    @Override
    public String getActionString(Map<String, Object> properties) {
        return String.format("export function onChange%s(data) {\n" +
                "    return {\n" +
                "        type : ActionType.CHANGE_%s_FOR_%s,\n" +
                "        data\n" +
                "    }\n" +
                "}", this.getFieldFirstUpperCamelCaseName(), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"));
    }

    @Override
    public String getActionTypeString(Map<String, Object> properties) {
        return String.format("export const CHANGE_%s_FOR_%s = \"CHANGE_%s_FOR_%s\";\n", this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"));
    }

    @Override
    public String getReducerSwitches(Map<String, Object> properties) {
        return String.format("case ActionType.CHANGE_%s_FOR_%s:\n" +
                        "            return handleChange%sFor%s(state, action);",
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"));
    }

    @Override
    public String getReducerFunctions(Map<String, Object> properties) {
        return String.format("function handleChange%sFor%s(state, action) {\n" +
                "    const newCondition = Object.assign({}, state.searchCondition, {\n" +
                "        %s : action.data.id\n" +
                "    });\n" +
                "\n" +
                "    return Object.assign({}, state, {\n" +
                "        searchCondition : newCondition\n" +
                "    });\n" +
                "}", this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"), this.getFieldName());
    }

    @Override
    public List<String> getReducerInitState(Map<String, Object> properties) {
        return Arrays.asList(
                String.format("%sList: [ { name: '全部%s', id: 0 } ]", this.getFieldName(), this.getFieldName())
        );
    }

    @Override
    public List<String> getCustomImportStatement() {
        return Arrays.asList(
                "import Select from \"react-select\""
        );
    }
}
